package com.wzl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzl.dao.ContactsInfoMapper;
import com.wzl.entity.dto.ContactsInfoDTO;
import com.wzl.entity.vo.PatientInfoVO;
import com.wzl.entity.vo.SysDictDataVO;
import com.wzl.responsitory.SelectCode;
import com.wzl.service.ContactsInfoService;
import com.wzl.service.PatientInfoService;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ContactsInfoServiceImpl extends ServiceImpl<ContactsInfoMapper, ContactsInfoDTO> implements ContactsInfoService {

    @Autowired
    private SelectCode selectCode;

    @Autowired
    private PatientInfoService patientInfoService;

    @Override
    public Page<ContactsInfoDTO> getPage(Page<ContactsInfoDTO> page, ContactsInfoDTO contactsInfoDTO) {
        QueryWrapper<ContactsInfoDTO> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(contactsInfoDTO.getId())) {
            queryWrapper.lambda().like(ContactsInfoDTO::getId, contactsInfoDTO.getId());
        }
        if (StringUtils.isNotEmpty(contactsInfoDTO.getName())) {
            queryWrapper.lambda().like(ContactsInfoDTO::getName, contactsInfoDTO.getName());
        }
        Page<ContactsInfoDTO> contactsInfoDTOPage = this.baseMapper.selectPage(page, queryWrapper);
        conversion(contactsInfoDTOPage.getRecords());
        return contactsInfoDTOPage;
    }

    @Override
    public boolean saveContactsInfo(ContactsInfoDTO contactsInfoDTO) {
        String startId = RandomStringUtils.randomAlphanumeric(6);
        int length = contactsInfoDTO.getIdCard().length();
        if (length < 6) {
            return false;
        }
        String endId = contactsInfoDTO.getIdCard().substring(length - 6, length);
        StringBuilder stringBuffer = new StringBuilder();
        // 设置患者编号
        contactsInfoDTO.setId(stringBuffer.append(startId).append(endId).toString());
        return this.baseMapper.insert(contactsInfoDTO) > 0;
    }

    @Override
    public ContactsInfoDTO contactDetail(String id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public boolean deleteContact(String id) {
        return this.baseMapper.deleteById(id) > 0;
    }

    @Override
    public boolean updateContact(ContactsInfoDTO contactsInfoDTO) {
        return this.baseMapper.updateById(contactsInfoDTO) > 0;
    }

    private void conversion(List<ContactsInfoDTO> info) {
        List<PatientInfoVO> patientInfo = patientInfoService.getPatientInfo();
        List<String> dicCode = Arrays.asList("sex", "countyArea");
        Map<String, List<SysDictDataVO>> listMap = selectCode.dealAllCode(dicCode);
        info.forEach(data -> {
            for (SysDictDataVO sysDictDataVO : listMap.get("sex")) {
                if (StringUtils.equals(sysDictDataVO.getValue(), data.getSex())) {
                    data.setSex(sysDictDataVO.getLabel());
                    break;
                }
            }
            for (SysDictDataVO sysDictDataVO : listMap.get("countyArea")) {
                if (StringUtils.equals(sysDictDataVO.getValue(), data.getBelongArea())) {
                    data.setBelongArea(sysDictDataVO.getLabel());
                    break;
                }
            }
            for (PatientInfoVO patientInfoVO : patientInfo) {
                if (StringUtils.equals(patientInfoVO.getId(), data.getPatientInfoId())) {
                    data.setPatientInfoId(patientInfoVO.getName());
                    break;
                }
            }
        });
    }

}
